package com.sanqing.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.sanqing.base.action.BaseAction;
import com.sanqing.po.Course;
import com.sanqing.po.Examine;
import com.sanqing.po.ExamineHis;
import com.sanqing.po.Questions;
import com.sanqing.po.Score;
import com.sanqing.po.Subject;
import com.sanqing.po.User;
import com.sanqing.service.CourseService;
import com.sanqing.service.ExamineHisService;
import com.sanqing.service.ExamineService;
import com.sanqing.service.ScoreService;
import com.sanqing.service.SubjectService;
import com.sanqing.util.Page;
import com.sanqing.util.PageResult;

@Results({ @Result(name = "list", location = "/teacher/score/score-list.jsp"), 
	       @Result(name = "examList", location = "/teacher/exam/examList.jsp"), 
	       @Result(name = "rule", location = "/teacher/exam/rule.jsp"), 
	       @Result(name = "video", location = "/teacher/exam/video.jsp"), 
	       @Result(name = "examSubject", location = "/teacher/exam/index.jsp"),
	       @Result(name = "submitExam", location = "/teacher/exam/examResult.jsp"),
	       @Result(name = "listExp", location = "/teacher/score/score-list-exp.jsp"), 
	       @Result(name = "view", location = "/teacher/exam/view.jsp"), 
	       @Result(name = "exercise", location = "/teacher/exam/exercise.jsp"), 
	       @Result(name = "submitExamExer", location = "/teacher/exam/submitExamExer.jsp"), })
public class ScoreAction extends BaseAction<Score> {

	private static final long serialVersionUID = -6961985576821652833L;
	private Score score = new Score();
	private int currentPage;
	@Autowired
	private ScoreService scoreService;

	@Autowired
	private SubjectService subjectService;

	@Autowired
	private CourseService courseService;

	@Autowired
	private ExamineHisService examineHisService;

	@Autowired
	private ExamineService examineService;
	/**
	 * 成绩查询
	 */
	@Override
	public String list() throws Exception {
		User sysUser = (User) getSession().getAttribute("session_user_key");
		Page page = new Page();
		page.setEveryPage(10);// 每页显示10条记录
		page.setCurrentPage(currentPage);// 设置当前页
		PageResult pageResult = scoreService.queryScoreByPage(page, score, sysUser);
		getRequest().setAttribute("pageResult", pageResult);
		if (score.getUser() != null) {
			getRequest().setAttribute("name", score.getUser().getName());
		}
		getRequest().setAttribute("score", score);
		return "list";
	}

	public String listExp() {
		User sysUser = (User) getSession().getAttribute("session_user_key");
		List<Score> scores = scoreService.getList(sysUser);
		getRequest().setAttribute("list", scores);
		return "listExp";
	}

	/**
	 * 学生查询自己的成绩
	 * 
	 * @return
	 */
	public String myExamList() {
		User sysUser = (User) getSession().getAttribute("session_user_key");
		List<Map<String, Object>> list = scoreService.getMyExamList(sysUser);
		getRequest().setAttribute("list", list);
		return "examList";
	}
    /**
     * 开始考试
     * @return
     */
	public String getExamSubject() {
		Examine examine = examineService.getById(score.getId());
		
		List<Questions> chooseSubject = examineService.getExamineSubject("1", examine.getCourse());
		List<Questions> decideList = examineService.getExamineSubject("2", examine.getCourse());
		List<Questions> doubleDecideList = examineService.getExamineSubject("3", examine.getCourse());

		getRequest().setAttribute("chooseSubject", chooseSubject);
		getRequest().setAttribute("decideList", decideList);
		getRequest().setAttribute("doubleDecideList", doubleDecideList);
		getRequest().setAttribute("courseId", examine.getCourse().getCourseId());
		getRequest().setAttribute("examine", examine);
		String examineBatchId = getRequest().getParameter("examineBatchId");
		getRequest().setAttribute("examineBatchId", examineBatchId);
		return "examSubject";
	}

	public String getRule() {
		String id = score.getId();
		String examineBatchId = getRequest().getParameter("examineBatchId");
		getRequest().setAttribute("examineBatchId", examineBatchId);
		return "rule";
	}

	public String getVideo() {
		String id = score.getId();
		String examineBatchId = getRequest().getParameter("examineBatchId");
		getRequest().setAttribute("examineBatchId", examineBatchId);
		return "video";
	}

	/**
	 * 在线考试
	 * 
	 * @return
	 */
	public String submitExam() {
		String examineId =getRequest().getParameter("examineId");
		String examineBatchId = getRequest().getParameter("examineBatchId");
		int result = 0;
		User sysUser = (User) getSession().getAttribute("session_user_key");
		String courseId = getRequest().getParameter("courseId");
		String[] subjectId = getRequest().getParameterValues("subjectID");
		List<ExamineHis> examineHis = new ArrayList<ExamineHis>();
		if (subjectId != null && subjectId.length > 0) {
			for (String sub : subjectId) {
				Subject subject = subjectService.getById(sub);
				ExamineHis his = new ExamineHis();
				his.setSubject(subject);
				his.setResult("0");
				if ("3".equals(subject.getSubjectType())) {// 多选题
					String[] answer = null;
					String key = "";
					answer = getRequest().getParameterValues("subjectAnswer" + sub);
					if (answer != null && answer.length > 0) {
						for (String string : answer) {
							key += string;
						}
						his.setUserAnswer(key);
						if (key.equals(subject.getSubjectAnswer())) {
							result += 5;// 多选一个5分
							his.setResult("1");
						}
					}
				} else {
					String answer = getRequest().getParameter("subjectAnswer" + sub);
					his.setUserAnswer(answer);
					if (answer.equals(subject.getSubjectAnswer())) {
						if ("1".equals(subject.getSubjectType())) {
							result += 3;// 单选一个3分
							his.setResult("1");
						} else {
							result += 2;// 单选一个2分
							his.setResult("1");
						}
					}
				}
				examineHis.add(his);
			}
		}
		Course course = courseService.getById(courseId);
		scoreService.accountResult(result, course, sysUser, examineHis,examineBatchId);

		String msg = "";
		String flag = "";
		if (result < 60) {
			msg = "你没有通过考试,可以继续参加考试！";
			flag = "0";
		} else {
			msg = "感谢你，顺利通过考试！";
			flag = "1";
		}
		getRequest().setAttribute("result", result);
		getRequest().setAttribute("msg", msg);
		getRequest().setAttribute("flag", flag);
		getRequest().setAttribute("courseId", courseId);
		getRequest().setAttribute("examineId", examineId);
		return "submitExam";
	}

	/**
	 * 查看考题
	 * 
	 * @return
	 */
	public String view() {
		List<ExamineHis> chooseSubject = examineHisService.getList(score.getId(), "1");
		List<ExamineHis> decideList = examineHisService.getList(score.getId(), "2");
		;
		List<ExamineHis> doubleDecideList = examineHisService.getList(score.getId(), "3");
		getRequest().setAttribute("chooseSubject", chooseSubject);
		getRequest().setAttribute("decideList", decideList);
		getRequest().setAttribute("doubleDecideList", doubleDecideList);
		getRequest().setAttribute("score", scoreService.getByKey(score.getId()));
		return "view";
	}

	/**
	 * 练习
	 * 
	 * @return
	 */
	public String exercise() {
		List<Subject> chooseSubject = scoreService.getSubject("1", score.getId());
		List<Subject> decideList = scoreService.getSubject("2", score.getId());
		List<Subject> doubleDecideList = scoreService.getSubject("3", score.getId());
		getRequest().setAttribute("chooseSubject", chooseSubject);
		getRequest().setAttribute("decideList", decideList);
		getRequest().setAttribute("doubleDecideList", doubleDecideList);
		getRequest().setAttribute("courseId", score.getId());
		return "exercise";
	}

	/**
	 * 查看练习结果
	 * 
	 * @return
	 */
	public String submitExamExer() {
		int result = 0;
		String courseId = getRequest().getParameter("courseId");
		String[] subjectId = getRequest().getParameterValues("subjectID");
		List<ExamineHis> chooseSubject = new ArrayList<ExamineHis>();
		List<ExamineHis> decideList = new ArrayList<ExamineHis>();
		List<ExamineHis> doubleDecideList = new ArrayList<ExamineHis>();
		if (subjectId != null && subjectId.length > 0) {
			for (String sub : subjectId) {
				Subject subject = subjectService.getById(sub);
				ExamineHis his = new ExamineHis();
				his.setSubject(subject);
				his.setResult("0");
				if ("3".equals(subject.getSubjectType())) {// 多选题
					String[] answer = null;
					String key = "";
					answer = getRequest().getParameterValues("subjectAnswer" + sub);
					if (answer != null && answer.length > 0) {
						for (String string : answer) {
							key += string;
						}
						his.setUserAnswer(key);
						if (key.equals(subject.getSubjectAnswer())) {
							result += 5;// 多选一个5分
							his.setResult("1");
						}
					}
					doubleDecideList.add(his);
				} else {
					String answer = getRequest().getParameter("subjectAnswer" + sub);
					his.setUserAnswer(answer);
					if (answer.equals(subject.getSubjectAnswer())) {
						if ("1".equals(subject.getSubjectType())) {
							result += 3;// 单选一个3分
							his.setResult("1");
							chooseSubject.add(his);
						} else {
							result += 2;// 单选一个2分
							his.setResult("1");
							decideList.add(his);
						}
					} else {
						if ("1".equals(subject.getSubjectType())) {
							chooseSubject.add(his);
						} else {
							decideList.add(his);
						}
					}

				}
			}
		}
		getRequest().setAttribute("result", result);
		getRequest().setAttribute("courseId", courseId);
		getRequest().setAttribute("chooseSubject", chooseSubject);
		getRequest().setAttribute("decideList", decideList);
		getRequest().setAttribute("doubleDecideList", doubleDecideList);
		return "submitExamExer";
	}

	@Override
	public Score getModel() {
		return score;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public Score getScore() {
		return score;
	}

	public void setScore(Score score) {
		this.score = score;
	}

}
